package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleResourceMapper roleResourceMapper;
    @Override
    public PageBean<RoleVo> pageQuery(Integer pageNum, Integer pageSize, RoleDto dto) {
        //开启分页
        PageHelper.startPage(pageNum, pageSize);
        // 查询
        Page<Role> page = roleMapper.selectPage(dto);
        return PageBean.of(page, RoleVo.class);
    }

    @Override
    public void create(RoleDto dto) {
        if(dto.getDataState()==null)
            dto.setDataState(SuperConstant.DATA_STATE_0);
        Role role = new Role();
        BeanUtils.copyProperties(dto, role);
        roleMapper.insert(role);
    }

    @Override
    public List<Long> findCheckedResources(Long roleId) {
        List<Long> resourceIds = roleMapper.findCheckedResources(roleId);
        return resourceIds;
    }

    @Override
    @Transactional
    public void updateRole(RoleDto dto) {
        //1. 更新角色基本信息，操作sys_role
        Role role = BeanUtil.toBean(dto, Role.class);
        roleMapper.updateByPrimaryKeySelective(role);

        //2. 更新角色对应的资源，操作sys_role_resource
        if (ObjectUtil.isNull(dto.getCheckedResourceNos())) {
            return;
        }

        //先删除角色与资源的对应关系（旧数据）
        roleResourceMapper.deleteByRoleId(dto.getId());

        //再删除角色与资源的对应关系（新数据）   String -> RoleResource
        Arrays.stream(dto.getCheckedResourceNos()).forEach(resourceNo -> {
            //将 资源编号 转换成  RoleResource 对象
            RoleResource roleResource = RoleResource.builder()
                    .roleId(dto.getId())
                    .resourceNo(resourceNo)
                    .dataState(SuperConstant.DATA_STATE_0)
                    .build();
            //添加
            roleResourceMapper.insert(roleResource);
        });
    }

    @Override
    @Transactional
    public void delete(List<Long> roleIds) {
        List<Long> rolesState = null;
        if (roleIds.size()!=0)
            rolesState = roleMapper.selectStates(roleIds);
        for (Long roleId : rolesState)
            if (roleId==0)
                throw new RuntimeException("角色状态为启用，不能删除");

        roleMapper.deleteBatchIds(roleIds);
        roleResourceMapper.deleteByRoleIds(roleIds);
    }

    @Override
    public List<RoleVo> initRoles() {
        List<Role> roles = new ArrayList<>();
        roles = roleMapper.list();
        List<RoleVo> roleVos = BeanUtil.copyToList(roles, RoleVo.class);
        return roleVos;
    }
}
